package com.tmall.wireless.virtualviewdemo.utils.imageloader.core;

import com.tmall.wireless.virtualviewdemo.utils.imageloader.core.assist.LoadedFrom;
import com.tmall.wireless.virtualviewdemo.utils.imageloader.core.process.BitmapProcessor;
import com.tmall.wireless.virtualviewdemo.utils.imageloader.utils.L;
import ohos.eventhandler.EventHandler;
import ohos.media.image.PixelMap;

final class ProcessAndDisplayImageTask implements Runnable {

    private static final String LOG_POSTPROCESS_IMAGE = "PostProcess image before displaying [%s]";

    private final ImageLoaderEngine engine;
    private final PixelMap bitmap;

    private final ImageLoadingInfo imageLoadingInfo;
    private final EventHandler handler;

    public ProcessAndDisplayImageTask(ImageLoaderEngine engine, PixelMap bitmap, ImageLoadingInfo imageLoadingInfo,
                                      EventHandler handler) {
        this.engine = engine;
        this.bitmap = bitmap;
        this.imageLoadingInfo = imageLoadingInfo;
        this.handler = handler;
    }

    @Override
    public void run() {
        L.d(LOG_POSTPROCESS_IMAGE, imageLoadingInfo.memoryCacheKey);

        BitmapProcessor processor = imageLoadingInfo.options.getPostProcessor();
        PixelMap processedBitmap = processor.process(bitmap);
        DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(processedBitmap, imageLoadingInfo, engine,
                LoadedFrom.MEMORY_CACHE);
        LoadAndDisplayImageTask.runTask(displayBitmapTask, imageLoadingInfo.options.isSyncLoading(), handler, engine);
    }
}
